package com.tjetc.view.teacherview;

import cn.hutool.json.JSONUtil;
import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.TestPaper;
import com.zhiyun.domain.TestQuestion;
import com.zhiyun.service.TestPaperService;
import com.zhiyun.service.TestQuestionService;
import com.zhiyun.vo.Result;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Exams_In_TestPaper {
    TestQuestionService testQuestionService=CilentProxy.getProxyInstance(TestQuestionService.class);
    TestPaperService testPaperService= CilentProxy.getProxyInstance(TestPaperService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course, TestPaper testPaper){
        while (true){
            System.out.println("~~~~~~~题库~~~~~~~~");
            System.out.println("1.查看详细题目");
            System.out.println("2.修改详细题目");
            System.out.println("3.删除详细题目");
            System.out.println("4.增加详细题目");
            System.out.println("0. 返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    findDetail(testPaper);
                    break;
                case 2:
                    updateDetail(testPaper);
                    break;
                case 3:
                    deleteDetail(testPaper);
                    break;
                case 4:
                    insertDetail(testPaper);
                    break;
                case 0:
                    return;
            }
        }
    }

    private void insertDetail(TestPaper testPaper) {
        while (true){




            testPaper.setTestQuestions(new ArrayList<TestQuestion>());
            String[] s=new String[]{"A","B","C","D","E","F","G"};
            while (true){
                System.out.println("是否添加题目(Y/N):");
                String next = scanner.next();
                if (next.equals("Y")){

                }else {
                    return;
                }
                //获得题目类型
                System.out.println("请输入题目类型(1.单选题 2.多选题 3.填空题 4.简答题)");
                int i = scanner.nextInt();
                //设置题目的类型
                if (i==1){
                    //获得题目
                    System.out.println("请输入题目:");
                    String Questionname = scanner.next();
                    //设置选项
                    List<String> stringList=new ArrayList<>();
                    int i1=0;
                    boolean as=true;
                    while (as){
                        i1++;
                        System.out.println("请输入选项:");
                        String option = scanner.next();
                        stringList.add(option);
                        if (i1>=2&&i1<s.length){
                            System.out.println("是否继续添加下一个选项(1 添加   0 取消添加 )");
                            int i2 = scanner.nextInt();
                            if (i2==1){}else {
                                as=false;
                            }
                        }else if (i1>s.length){
                            System.out.println("选项个数达到限制");
                        }
                    }
                    String s1 = JSONUtil.toJsonStr(stringList); //选项,JSON格式
                    String[] strings1 = JSONUtil.parseArray(s1).toArray(new String[0]);

                    String answerOption="";
                    String myAnswer = "";
                    boolean a=true;
                    for (int i2 = 0; i2 < strings1.length; i2++) {
                        System.out.println(s[i2]+". "+strings1[i2]);
                    }
                    while (a){
                        System.out.println("请输入答案的选项");
                        answerOption = scanner.next();
                        switch (answerOption) {
                            case "A":
                                if (strings1[0] != null){
                                    myAnswer = strings1[0];
                                    a=false;
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "B":
                                if (strings1[1] != null){
                                    myAnswer = strings1[1];
                                    a=false;
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "C":
                                if (strings1[2] != null){
                                    myAnswer = strings1[2];
                                    a=false;
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "D":
                                if (strings1[3] != null){
                                    myAnswer = strings1[3];
                                    a=false;
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "E":
                                if (strings1[4] != null){
                                    myAnswer = strings1[4];
                                    a=false;
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "F":
                                if (strings1[5] != null){
                                    myAnswer = strings1[5];
                                    a=false;
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            default:
                                System.out.println("没有该选项请重选");
                        }

                    }
                    List<String> answerList=new ArrayList<>();
                    answerList.add(myAnswer);
                    //答案 JSON格式
                    String answer = JSONUtil.toJsonStr(answerList);
                    //设置分值
                    System.out.println("请输入题目分值:");
                    int score = scanner.nextInt();
                    //设置题目所属的试卷
                    Integer testpaperid = testPaper.getId();
                    Result insert = testQuestionService.insert(new TestQuestion(null,
                            Questionname, s1, i, answer, score, testpaperid, null));
                    System.out.println(insert);

                }else if (i==2){

                    //获得题目
                    System.out.println("请输入题目:");
                    String Questionname = scanner.next();
                    //设置选项
                    List<String> stringList=new ArrayList<>();
                    int i1=0;
                    boolean as=true;
                    while (as){
                        i1++;
                        System.out.println("请输入第一个选项:");
                        String option = scanner.next();
                        stringList.add(option);
                        if (i1>=2&&i1<s.length){
                            System.out.println("是否继续添加下一个选项(1 添加   0 取消添加 )");
                            int i2 = scanner.nextInt();
                            if (i2==1){}else {
                                as=false;
                            }
                        }else if (i1>s.length){
                            System.out.println("选项个数达到限制");
                        }
                    }
                    String s1 = JSONUtil.toJsonStr(stringList); //选项,JSON格式
                    String[] strings1 = JSONUtil.parseArray(s1).toArray(new String[0]);

                    String answerOption="";
                    String myAnswer = "";
                    boolean a=true;
                    for (int i2 = 0; i2 < strings1.length; i2++) {
                        System.out.println(s[i2]+". "+strings1[i2]);
                    }
                    List<String> answerList=new ArrayList<>();
                    System.out.println("请输入答案的选项(-1退出)");
                    while (a){
                        answerOption = scanner.next();
                        switch (answerOption) {
                            case "A":
                                if (strings1[0] != null){
                                    myAnswer = strings1[0];
                                    answerList.add(myAnswer);
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "B":
                                if (strings1[1] != null){
                                    myAnswer = strings1[1];
                                    answerList.add(myAnswer);
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "C":
                                if (strings1[2] != null){
                                    myAnswer = strings1[2];
                                    answerList.add(myAnswer);
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "D":
                                if (strings1[3] != null){
                                    myAnswer = strings1[3];
                                    answerList.add(myAnswer);
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "E":
                                if (strings1[4] != null){
                                    myAnswer = strings1[4];
                                    answerList.add(myAnswer);
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "F":
                                if (strings1[5] != null){
                                    myAnswer = strings1[5];
                                    answerList.add(myAnswer);
                                }else {
                                    System.out.println("没有该选项请重选");
                                }
                                break;
                            case "-1":
                                a=false;
                                break;
                            default:
                                System.out.println("没有该选项请重选");
                        }

                    }
                    //答案 JSON格式
                    String answer = JSONUtil.toJsonStr(answerList);
                    //设置分值
                    System.out.println("请输入题目分值:");
                    int score = scanner.nextInt();
                    //设置题目所属的试卷
                    Integer testpaperid = testPaper.getId();
                    Result insert = testQuestionService.insert(new TestQuestion(null,
                            Questionname, s1, i, answer, score, testpaperid, null));
                    System.out.println(insert);

                }else if (i==3){



                    //获得题目
                    System.out.println("请输入题目:");
                    String Questionname = scanner.next();
                    System.out.println("请输入答案");
                    String myAnswer = scanner.next();
                    List<String> answerList=new ArrayList<>();
                    answerList.add(myAnswer);
                    //答案 JSON格式
                    String answer = JSONUtil.toJsonStr(answerList);
                    //设置分值
                    System.out.println("请输入题目分值:");
                    int score = scanner.nextInt();
                    //设置题目所属的试卷
                    Integer testpaperid = testPaper.getId();
                    Result insert = testQuestionService.insert(new TestQuestion(null,
                            Questionname, null, i, answer, score, testpaperid, null));
                    System.out.println(insert);






                }else if (i==4){


                    //获得题目
                    System.out.println("请输入题目:");
                    String Questionname = scanner.next();
                    System.out.println("请输入答案");
                    String myAnswer = scanner.next();
                    List<String> answerList=new ArrayList<>();
                    answerList.add(myAnswer);
                    //答案 JSON格式
                    String answer = JSONUtil.toJsonStr(answerList);
                    //设置分值
                    System.out.println("请输入题目分值:");
                    int score = scanner.nextInt();
                    //设置题目所属的试卷
                    Integer testpaperid = testPaper.getId();
                    Result insert = testQuestionService.insert(new TestQuestion(null,
                            Questionname, null, i, answer, score, testpaperid, null));
                    System.out.println(insert);




                }
                break;
            }






        }
    }

    private void deleteDetail(TestPaper testPaper) {
        while (true){
            Result result = testQuestionService.selectByPaperId(testPaper.getId());
            if (result.getCode()==200){
                List<TestQuestion> obj = (List<TestQuestion>) result.getObj();
                for (TestQuestion testQuestion : obj) {
                    System.out.println("    题目id:   "+testQuestion.getId()+"题目:  "+testQuestion.getTitle());
                    String[] strings=new String[]{"A","B","C","D","E","F"};
                    if (testQuestion.getOptions()!=null){
                        String options = testQuestion.getOptions();
                        String[] strings1 = JSONUtil.parseArray(options).toArray(new String[0]);
                        for (int i = 0; i < strings1.length; i++) {
                            System.out.println( strings[i]+ ". "+strings1[i]);
                        }
                    }
                    System.out.println("答案: "+testQuestion.getAnswer());
                }
                System.out.println("请输入想要删除的题目(-1退出)");
                int id = scanner.nextInt();
                if (id==-1){
                    return;
                }
                Result delete = testQuestionService.delete(id);
                System.out.println(delete.getMsg());

            }else {
                System.out.println(result.getMsg());
            }
        }
    }

    private void updateDetail(TestPaper testPaper) {
        while (true){
            Result result = testQuestionService.selectByPaperId(testPaper.getId());
            if (result.getCode()==200){
                List<TestQuestion> obj = (List<TestQuestion>) result.getObj();
                for (TestQuestion testQuestion : obj) {
                    System.out.println("题目id:"+testQuestion.getId()+"      题目:  "+testQuestion.getTitle());
                    String[] strings=new String[]{"A","B","C","D","E","F"};
                    if (testQuestion.getOptions()!=null){
                        String options = testQuestion.getOptions();
                        String[] strings1 = JSONUtil.parseArray(options).toArray(new String[0]);
                        for (int i = 0; i < strings1.length; i++) {
                            System.out.println( strings[i]+ ". "+strings1[i]);
                        }
                    }
                    System.out.println("答案: "+testQuestion.getAnswer());
                }
                System.out.println("请输入想要修改的题目id");
                int id = scanner.nextInt();
                //获得题目对象testQuestion1
                TestQuestion testQuestion1 = null;
                for (TestQuestion testQuestion : obj) {
                    if (testQuestion.getId()==id){
                        testQuestion1=testQuestion;
                    }
                }
                if (testQuestion1!=null){
                    System.out.println("请输入修改后的题目名称");
                    String next = scanner.next();
                    testQuestion1.setTitle(next);
                    //正确答案
                    String answer=null;
                    //选项修改
                    String[] strings=null;
                    Integer score=null;
                    if (testQuestion1.getOptions()!=null){
                        System.out.println("原选项:");
                       strings = JSONUtil.parseArray(testQuestion1.getOptions()).toArray(new String[0]);
                        for (int i = 0; i < strings.length; i++) {
                            System.out.println((i+1)+". "+strings[i]);
                        }
                        while (true){
                            System.out.println("请输入想要修改第几个选项(-1退出)");
                            int i = scanner.nextInt();
                            if (i==-1){
                                break;
                            }
                            if (i>0&&i<=strings.length){
                                System.out.println("原选项: "+strings[i-1]);
                                System.out.println("修改后的选项: ");
                                String next1 = scanner.next();
                                System.out.println("是否是正确答案(Y/N)");
                                String next2 = scanner.next();
                                if (next2.equals("Y")){
                                    answer=next2;
                                }
                                strings[i-1]=next1;
                            }else {
                                System.out.println("输入的选项不存在,请重新输入");
                            }
                        }
                    }
                    //将修改后的选项初始化到题目对象testQuestion1中
                    if (strings!=null){
                        String options = JSONUtil.toJsonStr(strings);
                        testQuestion1.setOptions(options);
                        if (answer!=null){
                            testQuestion1.setAnswer(JSONUtil.toJsonStr(answer));
                        }
                        System.out.println("请输入修改后的分值");
                        int i = scanner.nextInt();
                        score=i;
                        testQuestion1.setScore(i);
                    }else {
                        System.out.println("请输入修改后的答案");
                        answer = scanner.next();
                        testQuestion1.setAnswer(answer);
                        List<String> strings1 = new ArrayList<>();
                        strings1.add(answer);
                        String newAnswerjson = JSONUtil.toJsonStr(strings1);
                        testQuestion1.setAnswer(newAnswerjson);
                        System.out.println("请输入修改后的分值");
                        int i = scanner.nextInt();
                        testQuestion1.setScore(i);
                    }
                    Result update = testQuestionService.update(testQuestion1);
                    System.out.println(update.getMsg());

                }
            }else {
                System.out.println(result.getMsg());
            }
            break;
        }

    }

    private void findDetail(TestPaper testPaper) {
        while (true){
            Result result = testQuestionService.selectByPaperId(testPaper.getId());
            if (result.getCode()==200){
                List<TestQuestion> obj = (List<TestQuestion>) result.getObj();
                for (TestQuestion testQuestion : obj) {
                    System.out.println("题目:  "+testQuestion.getTitle());
                    String[] strings=new String[]{"A","B","C","D","E","F"};
                    if (testQuestion.getOptions()!=null){
                        String options = testQuestion.getOptions();
                        String[] strings1 = JSONUtil.parseArray(options).toArray(new String[0]);
                        for (int i = 0; i < strings1.length; i++) {
                            System.out.println( strings[i]+ ". "+strings1[i]);
                        }
                    }
                    System.out.println("答案: "+testQuestion.getAnswer());
                }
            }else {
                System.out.println(result.getMsg());
            }
            break;
        }
    }
}
